/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/03/09
* File: ProgramAttribute.cpp
* Blog: http://www.cnblogs.com/zengqh/
**/

#include "OGLProgramAttribute.h"
#include <render_system/BaseRenderSystem/VertexBuffer.h>
#include "OGL.h"

namespace HY
{

OGLProgramAttribute::OGLProgramAttribute(int32 loc)
	: loc_(loc)
{

}

OGLProgramAttribute::~OGLProgramAttribute()
{

}

bool OGLProgramAttribute::set(VertexBuffer* vb, uint32 idx)
{
	if (!vb)
	{
		glDisableVertexAttribArray(loc_);
		return false;
	}

	const VertexBuffer::Attribute* attr = vb->getVertexAttribute(idx);
	if (!attr)
	{
		glDisableVertexAttribArray(loc_);
		return false;
	}

	vb->bind();

	glVertexAttribPointer(loc_, attr->components_api, 
		attr->type_api_, GL_FALSE, vb->getVertexSize(), (const void*)attr->offset_);

	glEnableVertexAttribArray(loc_);

	return true;
}

bool OGLProgramAttribute::set(VertexBuffer* vb, uint32 semantic, uint32 channel)
{
	if (!vb)
	{
		glDisableVertexAttribArray(loc_);
		return false;
	}

	const VertexBuffer::Attribute* attr = vb->getVertexAttribute(semantic, channel);
	if (!attr)
	{
		glDisableVertexAttribArray(loc_);
		return false;
	}

	vb->bind();

	glVertexAttribPointer(loc_, attr->components_api, 
		attr->type_api_, GL_FALSE, vb->getVertexSize(), (const void*)attr->offset_);

	glEnableVertexAttribArray(loc_);

	return true;
}

}